package com.foreknow.dao.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Guestbook;

import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl extends BaseDao implements GuestbookDao {
    @Override
    public boolean save(Guestbook gb) throws SQLException {
        String sql = "insert into guestbook(name,phone,email,title,content,createdtime) values(?,?,?,?,?,?)";
        int isRight = jdbcTemplate.update(sql,gb.getName(),gb.getPhone(),gb.getEmail(),gb.getTitle(),gb.getContent(),gb.getCreatedtime());
        return isRight == 1;
    }

    @Override
    public List<Object> queryAll() {
        String sql = "select * from guestbook";
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping);
        return list;
    }

    @Override
    public boolean deleteById(int id) throws SQLException {
        String sql = "delete from guestbook where id=?";
        int isRight = jdbcTemplate.update(sql,id);
        return isRight == 1;
    }

    @Override
    public Guestbook getQueryById(int id) {
        String sql = "select * from guestbook where id=?";
        EntityMapping mapping = mappingFactory.getMapping(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping,id);
        if(list.size()>0) {
            return (Guestbook) list.get(0);
        }
        return null;
    }
}
